package com.ycrlkj.android.modulespublic.common.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.ycrlkj.android.modulespublic.common.R;

import java.util.ArrayList;
import java.util.List;

public class DataBindingRecyclerViewAdapter<T> extends RecyclerView.Adapter<DataBindingRecyclerViewAdapter.ItemViewHolder> {

    private final LayoutInflater inflater;
    private List<T> list;
    private final int layoutId; //布局文件id(R.layout.item_user)
    private final int variableId; //item绑定变量的变量id(BR.user)
    private OnItemClickListener<T> onItemClickListener; //item点击事件
    private int rid; //为item子控制添加点击事件, 子控制的rid(R.id.btn_editUser)
    private OnItemChildClickListener<T> onItemChildClickListener; //item上子控制的点击事件

    //在 activity 中使用的示例代码
    //adapter = new DataBindingRecyclerViewAdapter<>(this, R.layout.item_user, BR.user, R.id.btn_edit);

    public DataBindingRecyclerViewAdapter(Context context, int layoutId, int variableId) {
        this.inflater = LayoutInflater.from(context);
        this.list = new ArrayList<>();
        this.layoutId = layoutId;
        this.variableId = variableId;
    }

    public DataBindingRecyclerViewAdapter(Context context, int layoutId, int variableId, OnItemClickListener<T> onItemClickListener) {
        this.inflater = LayoutInflater.from(context);
        this.list = new ArrayList<>();
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.setOnItemClickListener(onItemClickListener);
    }

    public DataBindingRecyclerViewAdapter(Context context, int layoutId, int variableId, int rid) {
        this.inflater = LayoutInflater.from(context);
        this.list = new ArrayList<>();
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.rid = rid;
    }

    public DataBindingRecyclerViewAdapter(Context context, int layoutId, int variableId, int rid, OnItemChildClickListener<T> onItemChildClickListener) {
        this.inflater = LayoutInflater.from(context);
        this.list = new ArrayList<>();
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.rid = rid;
        this.onItemChildClickListener = onItemChildClickListener;
    }

    public DataBindingRecyclerViewAdapter(Context context, int layoutId, int variableId, OnItemClickListener<T> onItemClickListener, int rid, OnItemChildClickListener<T> onItemChildClickListener) {
        this.inflater = LayoutInflater.from(context);
        this.list = new ArrayList<>();
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.setOnItemClickListener(onItemClickListener);
        this.rid = rid;
        this.onItemChildClickListener = onItemChildClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutId, viewGroup, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder viewHolder, @SuppressLint("RecyclerView") int position) {

        T item = list.get(position);

        ViewDataBinding viewHolderBinding = viewHolder.getBinding();
        viewHolderBinding.setVariable(variableId, item);
        viewHolderBinding.executePendingBindings();

        if(onItemClickListener != null){
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(item, position);
                    }
                }
            });
        }

        //只有传了子控制的 rid 和 子控制点击事件, 才执行给子控制加事件
        if(rid != 0 && onItemChildClickListener != null){
            viewHolder.itemView.findViewById(rid).setOnClickListener((new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemChildClickListener != null) {
                        onItemChildClickListener.onItemChildClickListener(item, position);
                    }
                }
            }));

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public List<T> getItemList(){
        return list;
    }

    //@SuppressLint("notifyItemInserted")
    @SuppressLint("notifyDataSetChanged")
    public void add(T item) {
        int index = this.list.size();//原列表长度
        this.list.add(item);
        //notifyItemInserted(index);//局部刷新, 插入一个(参数为插入的起始位置:即列表末尾)
        //notifyItemRangeChanged(index - 1, index);//需要对改变的部分刷新一次才会有效, 否则position错乱

        notifyDataSetChanged();//全部刷新
    }

    //@SuppressLint("notifyItemRangeInserted")
    @SuppressLint("notifyDataSetChanged")
    public void add(List<T> list) {
        int index = this.list.size();//原列表长度
        int size = list.size();//插入列表长度
        this.list.addAll(list);
        //notifyItemRangeInserted(index, size);//局部刷新, 插入多个(参数为 插入的起始位置:即列表末尾, 插入的数目:即插入数组的长度)
        //notifyItemRangeChanged(index - 1, index + size);//需要对改变的部分刷新一次才会有效, 否则position错乱

        notifyDataSetChanged();//全部刷新
    }

    //@SuppressLint("notifyItemRangeInserted")
    @SuppressLint("notifyDataSetChanged")
    public void replace(List<T> list) {
        int index = 0;
        int size = list.size();//插入列表长度
        this.list = list;
        //notifyItemRangeInserted(index, size);//局部刷新, 插入多个(参数为 插入的起始位置:即列表末尾, 插入的数目:即插入数组的长度)
        //notifyItemRangeChanged(index - 1, index + size);//需要对改变的部分刷新一次才会有效, 否则position错乱

        notifyDataSetChanged();//全部刷新
    }

    //@SuppressLint("notifyItemRemoved")
    @SuppressLint("notifyDataSetChanged")
    public void remove(int position) {
        int index = this.list.size();//原列表长度
        if (index <= 0) {
            return;
        }
        this.list.remove(position);
        //notifyItemRemoved(position);
        //notifyItemRangeChanged(position - 1, index - 1);//需要对改变的部分刷新一次才会有效, 否则position错乱

        notifyDataSetChanged();//全部刷新
    }

    @SuppressLint("NotifyDataSetChanged")
    public void clear() {
        this.list.clear();
        notifyDataSetChanged();//全部刷新
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final ViewDataBinding binding;

        public ItemViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return this.binding;
        }
    }


    public interface OnItemClickListener<T> {
        void onItemClickListener(T item, int position);
    }
    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }



    public interface OnItemChildClickListener<T> {
        void onItemChildClickListener(T item, int position);
    }
    public void setOnItemChildClickListener(OnItemChildClickListener<T> onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
    }

}

